package activity.Left.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hichip.campro.R;

import java.io.File;
import java.util.List;

import activity.Left.LeftLocalFileActivity;
import bean.LeftLocalFileBean;
import bean.MyCamera;
import common.HiDataValue;
import custom.dialog.NiftyDialogBuilder;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import utils.HiTools;

public class LocalFileListAdapter extends BaseAdapter {
    private List<LeftLocalFileBean> beans;
    private Context mContext;
    private boolean mIsDetele=false;//当前是否是删除状态

    public LocalFileListAdapter(Context context,List<LeftLocalFileBean> beans) {
        this.beans=beans;
        this.mContext=context;
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public LeftLocalFileBean getItem(int position) {
        return beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=View.inflate(mContext, R.layout.item_local_file,null);
            holder=new ViewHolder();
            holder.iv_picture=convertView.findViewById(R.id.iv_picture);
            holder.tv_file_size=convertView.findViewById(R.id.tv_file_size);
            holder.tv_uid=convertView.findViewById(R.id.tv_uid);
            holder.tv_photo_num=convertView.findViewById(R.id.tv_photo_num);
            holder.tv_video_num=convertView.findViewById(R.id.tv_video_num);
            holder.iv_item_delete=convertView.findViewById(R.id.iv_item_delete);
            holder.tv_nickname=convertView.findViewById(R.id.tv_nickname);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        LeftLocalFileBean bean=beans.get(position);
        holder.tv_file_size.setText(bean.getSize());
        holder.tv_uid.setText(bean.getUid());
        Glide.with(mContext).load(new File(bean.getPath())).placeholder(R.mipmap.oc_local_video).error(R.mipmap.oc_local_video)
                .bitmapTransform(new RoundedCornersTransformation(mContext, HiTools.dip2px(mContext,5),0)).into(holder.iv_picture);
        holder.tv_photo_num.setText(bean.getPhotoNum()+"");
        holder.tv_video_num.setText(bean.getVoideNum()+"");
        for(MyCamera camera: HiDataValue.CameraList){
            if(camera.getUid().equals(bean.getUid())){
                holder.tv_nickname.setText(camera.getNikeName());
                break;
            }
        }
        holder.iv_item_delete.setVisibility(mIsDetele?View.VISIBLE:View.GONE);
        holder.iv_item_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NiftyDialogBuilder dialog=NiftyDialogBuilder.getInstance(mContext);
                dialog.withMessageLayoutHWrapW(HiTools.dip2px(mContext,260));
                dialog.withTitle(mContext.getString(R.string.confirm_delete));
                dialog.withMessage(mContext.getString(R.string.delete_device_one)+bean.getUid()+mContext.getString(R.string.delete_device_two));
                dialog.withButton1Text(mContext.getString(R.string.cancel)).withButton2Text(mContext.getString(R.string.confirm));
                dialog.setButton1Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.setButton2Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        File file=bean.getFile();
                        if(file!=null&&file.exists()){
                            deleteFile(file);
                            beans.remove(bean);
                            mIsDetele=false;
                            notifyDataSetChanged();
                            if(beans.size()<1){
                                LeftLocalFileActivity act= (LeftLocalFileActivity) mContext;
                                act.gridview.setVisibility(View.GONE);
                                act.tv_nothing.setVisibility(View.VISIBLE);
                                act.title.setRightTxtGone();
                            }
                            if(onFileDeleteListener!=null){
                                onFileDeleteListener.onDelete(true);
                            }
                        }
                    }
                });
                dialog.show();
            }
        });
        return convertView;
    }

    public void setIsDetele(boolean mIsDetele){
        this.mIsDetele=mIsDetele;
        notifyDataSetChanged();
    }

    private class ViewHolder{
        ImageView iv_picture;
        TextView  tv_file_size;
        TextView  tv_uid;
        TextView  tv_photo_num;
        TextView  tv_video_num;
        ImageView iv_item_delete;
        TextView  tv_nickname;
    }

    //flie：要删除的文件夹的所在位置
    private void deleteFile(File file) {
        if(file==null||!file.exists()) return;
        if(file.isDirectory()){
            File[] files=file.listFiles();
            for(int i=0;i<files.length;i++){
                File f=files[i];
                deleteFile(f);
            }
            file.delete();
        }else {
            file.delete();
        }
    }

    public OnFileDeleteListener onFileDeleteListener;
    public interface  OnFileDeleteListener{
        void onDelete(boolean isDelete);
    }
    public void setOnFileDeleteListener(OnFileDeleteListener onFileDeleteListener){
        this.onFileDeleteListener=onFileDeleteListener;
    }
}
